<template>
    <el-form :model="form" :rules="rules" ref="menuForm" label-width="100px">
        <!-- 上级菜单 -->
        <el-form-item label="上级菜单" prop="parentMenu">
            <el-select v-model="form.parentMenu" placeholder="请选择上级菜单">
                <el-option label="主类目" value="0"></el-option>
                <el-option label="用户管理" value="1"></el-option>
                <el-option label="系统设置" value="2"></el-option>
            </el-select>
        </el-form-item>

        <!-- 菜单类型 - 核心切换逻辑 -->
        <el-form-item label="菜单类型" prop="menuType">
            <el-radio-group v-model="form.menuType" @change="handleMenuTypeChange">
                <el-radio label="catalog">目录</el-radio>
                <el-radio label="menu">菜单</el-radio>
                <el-radio label="button">按钮</el-radio>
            </el-radio-group>
        </el-form-item>

        <!-- 目录和菜单共有的字段 -->
        <template v-if="form.menuType === 'catalog' || form.menuType === 'menu'">
            <el-form-item label="菜单图标" prop="icon">
                <el-input v-model="form.icon" placeholder="例如：el-icon-menu"></el-input>
            </el-form-item>

            <el-form-item label="显示排序" prop="sort">
                <el-input-number v-model="form.sort" :min="1" :max="9999"></el-input-number>
            </el-form-item>
        </template>

        <!-- 只有目录和菜单需要的路由相关字段 -->
        <template v-if="form.menuType === 'catalog' || form.menuType === 'menu'">
            <!-- 组件路径 -->
             <el-form-item label="组件路径" prop="component" v-if="form.menuType === 'menu' || (form.menuType === 'catalog' && form.isExternal === 'true')"> 
                <el-input v-model="form.component"></el-input>
             </el-form-item>
            <el-form-item label="路由地址" prop="route"
                v-if="form.menuType === 'menu' || (form.menuType === 'catalog' && form.isExternal === 'true')">
                <el-input v-model="form.route" placeholder="请输入路由地址或外部链接"></el-input>
            </el-form-item>
        </template>

        <!-- 按钮特有的字段 -->
        <template v-if="form.menuType === 'button'">
            <el-form-item label="权限标识" prop="permission">
                <el-input v-model="form.permission" placeholder="例如：system:user:add"></el-input>
            </el-form-item>
        </template>

        <!-- 所有类型都需要的通用字段 -->
        <el-form-item label="菜单名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入菜单名称"></el-input>
        </el-form-item>

        <el-form-item label="显示状态" prop="visible">
            <el-radio-group v-model="form.visible">
                <el-radio label="show">显示</el-radio>
                <el-radio label="hide">隐藏</el-radio>
            </el-radio-group>
        </el-form-item>

        <el-form-item label="菜单状态" prop="status">
            <el-radio-group v-model="form.status">
                <el-radio label="normal">正常</el-radio>
                <el-radio label="disabled">停用</el-radio>
            </el-radio-group>
        </el-form-item>
    </el-form>
    <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
    </span>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { ElMessage } from 'element-plus'

const dialogVisible = ref(true)
const menuForm = ref(null)

// 表单数据
const form = reactive({
    parentMenu: '0',        // 上级菜单
    menuType: 'catalog',    // 菜单类型：catalog-目录，menu-菜单，button-按钮
    name: '',               // 菜单名称
    icon: '',               // 菜单图标
    sort: 1,                // 显示排序
    isExternal: 'false',    // 是否外链
    route: '',              // 路由地址
    visible: 'show',        // 显示状态
    status: 'normal',       // 菜单状态
    permission: '',         // 权限标识（按钮特有）
    buttonStyle: 'default'  // 按钮样式（按钮特有）
})

// 表单验证规则
const rules = reactive({
    name: [
        { required: true, message: '请输入菜单名称', trigger: 'blur' }
    ],
    menuType: [
        { required: true, message: '请选择菜单类型', trigger: 'change' }
    ],
    route: [
        {
            required: () => form.menuType === 'menu' || (form.menuType === 'catalog' && form.isExternal === 'true'),
            message: '请输入路由地址',
            trigger: 'blur'
        }
    ],
    permission: [
        {
            required: () => form.menuType === 'button',
            message: '请输入权限标识',
            trigger: 'blur'
        }
    ]
})

// 处理菜单类型变化
const handleMenuTypeChange = (value) => {
    // 重置与菜单类型相关的字段，避免数据混乱
    if (value === 'button') {
        form.icon = ''
        form.route = ''
        form.isExternal = 'false'
    } else if (value === 'catalog') {
        form.permission = ''
        form.buttonStyle = 'default'
    } else if (value === 'menu') {
        form.permission = ''
        form.buttonStyle = 'default'
    }

    // 重新验证表单
    menuForm.value && menuForm.value.validate()
}

// 监听是否外链变化，控制路由地址显示
watch(() => form.isExternal, (newVal) => {
    if (form.menuType === 'catalog' && newVal === 'true' && !form.route) {
        // 当目录设置为外链且路由为空时，给出提示
        ElMessage({
            message: '请输入外部链接地址',
            type: 'warning'
        })
    }
})

// 提交表单
const handleSubmit = () => {
    menuForm.value.validate((valid) => {
        if (valid) {
            // 这里可以添加提交数据到后端的逻辑
            console.log('提交的表单数据：', form)

            // 模拟提交成功
            ElMessage.success('菜单添加成功！')
            dialogVisible.value = false
        } else {
            ElMessage.error('请完善表单信息')
            return false
        }
    })
}
</script>

<style scoped>
.dialog-footer {
    text-align: right;
}
</style>
